package com.rxh.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class UUID {
    private static Map<String, Integer> IDENTITES = new HashMap<>();// 自增集合

    private static final int SHORT_KEY_MAX = 999;

    private static final int KEY_MAX = 9999;

    private static String IP;

    private static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyMMddHHmmss");

    private static SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("yyMMdd");

    static {
        IP = IpUtils.getIp();
        IP = IP.substring(IP.lastIndexOf(".") + 1);
        switch (IP.length()) {
            case 1:
                IP = "00" + IP;
                break;
            case 2:
                IP = "0" + IP;
                break;
            default:
                break;
        }
    }

    /**
     * 获取序列号
     *
     * @param table  表名
     * @param keyMax key最大值
     * @return key
     */
    private static synchronized int sequence(String table, int keyMax) {
        Integer identity = IDENTITES.get(table);
        if (null == identity) {
            identity = 0;
        }
        if (identity >= keyMax) {
            identity = 0;
        }
        IDENTITES.put(table, ++identity);
        return identity;
    }

    private static synchronized int sequence(int lastKey) {
        int identity = ++lastKey;
        if (identity >= UUID.SHORT_KEY_MAX) {
            identity = 0;
        }
        return identity;
    }

    /**
     * 获取 long 主键(12位)
     *
     * @param table 表名
     * @return 主键
     */
    public static long createShortKey(String table) {
        return Long.valueOf(TIME_FORMAT.format(new Date()) + IP + String.format("%03d", sequence(table, SHORT_KEY_MAX)));
    }

    /**
     * 获取 int 主键（7位yyMMdd000）
     *
     * @param lastId 最后的ID
     * @return 主键
     */
    public static int createIntegerKey(String lastId) {
        int lastIdDate = Integer.parseInt(lastId.substring(0, 6));
        int lastKey = Integer.parseInt(lastId.substring(6));
        int newIdDate = Integer.parseInt(TIME_FORMAT.format(new Date()));
        if (newIdDate > lastIdDate) {
            return Integer.parseInt(newIdDate + String.format("%03d", sequence(0)));
        }
        return Integer.parseInt(newIdDate + String.format("%03d", sequence(lastKey)));
    }

    /**
     * 获取 long 主键(19位)
     *
     * @param table 表名
     * @return 主键
     */
    public static long createKey(String table) {
        return Long.valueOf(DATE_FORMAT.format(new Date()) + IP + String.format("%04d", sequence(table, KEY_MAX)));
    }

    /**
     * 获取带前缀的主键(19位+前缀长度)
     *
     * @param table  表名
     * @param prefix 前缀名
     * @return 主键
     */
    public static String createKey(String table, String prefix) {
        return prefix + DATE_FORMAT.format(new Date()) + IP + String.format("%04d", sequence(table, KEY_MAX));
    }

    /**
     * 获取字符串的主键(最低19位)
     *
     * @param table  表名
     * @param length 指定长度
     * @return 主键
     */
    public static String createKey(String table, int length) {
        String fillStr = "";
        int fillLen = length - 19;
        if (fillLen > 0) {
            fillStr = String.format("%0" + fillLen + "d", 0);
        }
        return fillStr + DATE_FORMAT.format(new Date()) + IP + String.format("%04d", sequence(table, KEY_MAX));
    }

    /**
     * 获取指定长度 的带前缀主键(至少为19+前缀长度)
     *
     * @param table  表名
     * @param prefix 前缀名
     * @param length 指定长度
     * @return 主键
     */
    public static String createKey(String table, String prefix, int length) {
        String fillStr = "";
        int fillLen = length - 19 - prefix.length();
        if (fillLen > 0) {
            fillStr = String.format("%0" + fillLen + "d", 0);
        }
        return prefix + fillStr + DATE_FORMAT.format(new Date()) + IP + String.format("%04d", sequence(table, KEY_MAX));
    }
}
